import http from './http.js';

class CardPackage {
  constructor(data) {
    this.package_id = data.package_id;
    this.name = data.name;
    this.card_type = data.card_type;
    this.total_amount = data.total_amount;
    this.remaining_amount = data.remaining_amount;
    this.commission_amount = data.commission_amount || 0.0;
    this.expiry_date = data.expiry_date;
    this.customer_id = data.customer_id;
    this.store_id = data.store_id;
    this.created_at = data.created_at;
    this.updated_at = data.updated_at;
    this.is_deleted = data.is_deleted || false;
  }

  static fromApi(data) {
    return new CardPackage({
      package_id: data.package_id,
      name: data.name,
      card_type: data.card_type,
      total_amount: data.total_amount,
      remaining_amount: data.remaining_amount,
      commission_amount: data.commission_amount,
      expiry_date: data.expiry_date,
      customer_id: data.customer_id,
      store_id: data.store_id,
      created_at: data.created_at,
      updated_at: data.updated_at,
      is_deleted: data.is_deleted
    });
  }

  toApi() {
    return {
      name: this.name,
      card_type: this.card_type,
      total_amount: this.total_amount,
      commission_amount: this.commission_amount,
      expiry_date: this.expiry_date,
      customer_id: this.customer_id,
      store_id: this.store_id
    };
  }
}

export default {
  /**
   * 创建卡包
   * @param {Object} cardPackageData 卡包数据
   * @returns {Promise<CardPackage>}
   */
  async create(cardPackageData) {
    try {
      const response = await http.post('/card_packages/create', cardPackageData);
      return response.data;
    } catch (error) {
      console.error('创建卡包失败:', error);
      throw error;
    }
  },

  /**
   * 获取卡包详情
   * @param {string} packageId 卡包ID
   * @returns {Promise<CardPackage>}
   */
  async getById(packageId) {
    try {
      const response = await http.get(`/card_packages/detail/${packageId}`);
      return response.data;
    } catch (error) {
      console.error('获取卡包详情失败:', error);
      throw error;
    }
  },

  /**
   * 更新卡包信息
   * @param {string} packageId 卡包ID
   * @param {Object} updateData 更新数据
   * @returns {Promise<CardPackage>}
   */
  async update(packageId, updateData) {
    try {
      const response = await http.post(`/card_packages/update/${packageId}`, updateData);
      return response.data;
    } catch (error) {
      console.error('更新卡包失败:', error);
      throw error;
    }
  },

  /**
   * 删除卡包(软删除)
   * @param {string} packageId 卡包ID
   * @returns {Promise<void>}
   */
  async delete(packageId) {
    try {
      await http.post(`/card_packages/delete/${packageId}`);
    } catch (error) {
      console.error('删除卡包失败:', error);
      throw error;
    }
  },

  /**
   * 获取卡包列表
   * @param {Object} params 查询参数
   * @returns {Promise<{data: CardPackage[]}>}
   */
  async getList(params = {}) {
    try {
      const response = await http.get('/card_packages/list', { ...params });
      return  response.data;
    } catch (error) {
      console.error('获取卡包列表失败:', error);
      throw error;
    }
  },
  /**
   * 卡包充值
   * @param {Object} rechargeData 充值数据
   * @param {string} rechargeData.store_id 店铺ID
   * @param {string} rechargeData.user_id 客户ID
   * @param {string} rechargeData.package_id 卡包ID
   * @param {number} rechargeData.recharge_amount 充值金额
   * @param {number} [rechargeData.gift_amount] 赠送金额
   * @param {number} [rechargeData.discount_rate] 客户折扣比例
   * @returns {Promise<CardPackage>}
   */
  async recharge(rechargeData) {
    try {
      const response = await http.post('/card_packages/recharge', rechargeData);
      return response.data;
    } catch (error) {
      console.error('卡包充值失败:', error);
      throw error;
    }
  },

};